#!/bin/bash
#===============================================================================
#
#          FILE:  AKM-terminal
# 
#         USAGE:  sudo ./AKM-terminal 
# 
#   DESCRIPTION:  Automatically un-tars, compiles, and installs a kernel
# 
#  REQUIREMENTS:  See README
#        AUTHOR:  Anthony Byrne, abyrne561@gmail.com
#       VERSION:  1.3
#       CREATED:  10/04/2009 07:52:10 PM EDT
#       LICENSE:  GPL
#===============================================================================
################################################################################
# Auto Linux Kernel Maker (AKM) 
# Copyright 2009 Anthony Byrne
#
# This file is part of AKM
#
# AKM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# AKM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AKM.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

################################################################################
###~VARIABLES~###
################################################################################

RUNNING_KER=$(uname -r)
DIR=$(pwd)
COLOR=y
START_LOG="("
END_LOG=") 2>&1 | tee AKM.log"


# Text color variables
COLOR ()
{
if [[ "$COLOR" == "y" ]]; then
#  Output Colors
 
ERR=$(tput bold && tput setaf 1 && tput setb 7)
WARN=$(tput bold && tput setaf 4 && tput setb 7)
OP=$(tput bold && tput setaf 0 && tput setb 7)
#  Extra Colors (Not used but kept for future)
BLD=$(tput bold)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YLW=$(tput setaf 3)
BLUE=$(tput setaf 4)
PURPLE=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
ENDCLR=$(tput sgr0)
else
ERR=
WARN=
OP=
#  Extra Colors (Not used but kept for future)
BLD=
RED=
GREEN=
YLW=
BLUE=
PURPLE=
CYAN=
WHITE=
ENDCLR=
fi
}
################################################################################
###~ARGUMENTS~###
################################################################################

if [[ "$#" == "0" ]]; then
  NUM_ARGS=1
fi

if [[ "$#" == "1" ]]; then
  NUM_ARGS=1
fi
  
if [ $NUM_ARGS != 1 ]; then
    echo "${ERR}ERROR: Too Many Arguments${ENDCLR}"
    exit 0
fi

if [[ "$1" == "-h" ]]; then
  echo "Usage: AKM-terminal [OPTION]"
  echo "Automatically un-tars, compiles, and installs a kernel"
  echo ""
  echo "ARGUMENTS"
  echo "  -l          Show license and exit"
  echo "  -h          Show this help and exit"
  echo "  -v          Show version and exit"
  echo "  -d          Perform a dry run; run the script but don't actually do anything"
  echo "  --no-color  Run script without colored output"
  echo ""
  echo "Report bugs at Google Code Bug Tracker: http://code.google.com/p/auto-linux-kernel-maker/"
  exit 0
fi

if [[ "$1" == "-l" ]]; then

 echo Auto Linux Kernel Maker
 echo Copyright 2009 Anthony Byrne
 echo 
 echo This program is free software: you can redistribute it and/or modify
 echo it under the terms of the GNU General Public License as published by
 echo the Free Software Foundation, either version 3 of the License, or
 echo \(at your option\) any later version.
 echo
 echo This program is distributed in the hope that it will be useful,
 echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
 echo MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 echo GNU General Public License for more details.
 echo
 echo You should have received a copy of the GNU General Public License
 echo along with this program.  If not, see http://www.gnu.org/licenses/
 exit 0
fi
  
if [[ "$1" == "-v" ]]; then
  echo "1.3"
  exit 0
fi  

if [[ "$1" == "--no-color" ]]; then
  COLOR=n
fi 
COLOR

if [[ "$1" == "--no-log" ]]; then
  START_LOG=
  END_LOG=
fi 
################################################################################
###~FUNCTIONS~###
################################################################################

CHECKS ()
{
  # Show error if not root
  if [ $USER != root ]; then
    echo "${ERR}ERROR: Must Be Root${ENDCLR}"
    exit 0
  fi
  
  echo Copyright 2009 Anthony Byrne
  echo "This program comes with ABSOLUTELY NO WARRANTY"
  echo "This is free software, and you are welcome to redistribute it"
  echo "under certain conditions; run AKM with the -l option fo details"
    
  if [ $DIR != /usr/src ]; then
    IN_USR_SRC=n
    echo "${WARN}WARNING: You're not in /usr/src. It is highly recommended that you compile your kernel inside the /usr/src/ directory!${ENDCLR}"
    echo "${WARN}Continue? (y/n)${ENDCLR}"
    read NOSRCOP
  
  else

    IN_USR_SRC=y
    
  fi

  if [ $NOSRCOP != y ]; then
    echo "EXITING..."
    exit 0
  fi

}

CONFIG ()
{
  # Choose a Method
  echo ""
  echo ""
  echo "${OP}Choose a method for configuration${ENDCLR}"
  echo ""
  echo "${OP}1) Configure using the GUI (requires qt3-examples)${ENDCLR}" 
  echo "${OP}2) Configure using the Terminal${ENDCLR}"
  echo "${OP}3) Use a pre-existing config file${ENDCLR}"
  echo "${OP}4) Use the config file from your current running kernel${ENDCLR}"
  echo "${OP}Enter the number of your choice${ENDCLR}"
  read METHOD
  echo $METHOD

  if [[ "$METHOD" == "1" ]]; then
    make xconfig  
  fi

  if [[ "$METHOD" == "2" ]]; then
    xterm -e make config  
  fi

  if [[ "$METHOD" == "3" ]]; then
    echo "${WARN}You've set this script to use a preconfigured kernel config file. Put that config file in the linux-$KERNEL_VER directory that was just created. Make sure that file is named .config${ENDCLR}"

    read -n1 -r -p "Press any key to continue..." key
    xterm -e make oldconfig  
  fi
  
  if [[ "$METHOD" == "4" ]]; then
    echo "Getting Config From Kernel..."
    cp /usr/boot/config-${RUNNING_KER} -t .config
    sleep 2
    xterm -e make oldconfig
  fi
}

POST_INSTALL ()
{
  # Crestes a new Initial RAM File System (initramfs)
  echo "${OP}Running Post-Installation Tasks (Generating INITRAMFS)${ENDCLR}" 
  update-initramfs -k ${KERNEL_VER} -c

  # Creates links if in /usr/src
  if [ $IN_USR_SRC != n ]; then
    "${OP}Running Post-Installation Tasks (Creating File System Links)${ENDCLR}"
  ln -f -T /usr/src/linux-${KERNEL_VER} linux
  fi

  # Updates Bootloader
  echo "${OP}Running Post-Installation Tasks (Updating GRUB)${ENDCLR}"
  update-grub
}

MAKER ()
{
  # Compile the kernel (takes a long time)
  echo "${OP}Compiling the Kernel (takes a long time)${ENDCLR}"
  make 

  # Install the kernel modules
  echo "${OP}Installing Kernel Modules${ENDCLR}"
  make modules_install 

  # Install the kernel
  echo "${OP}Installing Kernel${ENDCLR}"
  make install 
}

CLEAN ()
{
  echo "${OP}Do you want to clean up?${ENDCLR}" 
  echo ""
  echo "${OP}1) No, Leave everything.${ENDCLR}"
  echo "${OP}2) Yes, just delete the un-needed files for me.${ENDCLR}"
  echo "${OP}3) Yes, but keep the .config file for the future.${ENDCLR}"
  echo "${OP}Enter the number of your choice${ENDCLR}"
  read DELETE
  echo $DELETE

  if [[ "$DELETE" == "2" ]]; then
    rm -r -f linux-$KERNEL_VER
  fi

  if [[ "$DELETE" == "3" ]]; then
    cp /boot/config-${KERNEL_VER} -t ~/
    rm -r -f linux-$KERNEL_VER
    echo "${OP}If you want to use the config file from this kernel in the furture, it is located in your home folder under the name of config-${KERNEL_VER}${ENDCLR}"
    sleep 2
  fi
}

DRYRUN ()
{
  (
  date >> AKM.log #Add time to log

  CHECKS
  
  echo ""
  echo "${WARN}Make sure that you launched this program from a directory which has a .tar.bz2 downloaded from kernel.org. If you didn't do this right, KILL this program, don't just hit cancel!${ENDCLR}"

  echo ""

  # Ask and fill variables
  echo "${OP}What is the version of the kernel you are trying to install? (e.g 2.6.0) MAKE SURE THIS IS RIGHT!${ENDCLR}"
  read KERNEL_VER
  echo $KERNEL_VER

  # Decompress
  echo "${OP}Decompressing Kernel...${ENDCLR}"

  # Choose a Method
  echo ""
  echo ""
  echo "${OP}Choose a method for configuration${ENDCLR}"
  echo ""
  echo "${OP}1) Configure using the GUI (requires qt3-examples)${ENDCLR}" 
  echo "${OP}2) Configure using the Terminal${ENDCLR}"
  echo "${OP}3) Use a pre-existing config file${ENDCLR}"
  echo "${OP}4) Use the config file from your current running kernel${ENDCLR}"
  echo "${OP}Enter the number of your choice${ENDCLR}"
  read METHOD
  echo $METHOD

  if [[ "$METHOD" == "1" ]]; then
    echo "Making Config..."
  fi

  if [[ "$METHOD" == "2" ]]; then
    echo "Making Config..."  
  fi

  if [[ "$METHOD" == "3" ]]; then
    echo "${WARN}You've set this script to use a preconfigured kernel config file. Put that config file in the linux-$KERNEL_VER directory that was just created. Make sure that file is named .config${ENDCLR}"

    read -n1 -r -p "Press any key to continue..." key
    echo "Making Config..."
  fi
  
  if [[ "$METHOD" == "4" ]]; then
    echo "Getting Config From Kernel..."
    sleep 2
    echo "Making Config..."
  fi

  # Compile the kernel (takes a long time)
  echo "${OP}Compiling the Kernel (takes a long time)${ENDCLR}"
  

  # Install the kernel modules
  echo "${OP}Installing Kernel Modules${ENDCLR}"
  

  # Install the kernel
  echo "${OP}Installing Kernel${ENDCLR}"
  
  echo "${OP}Do you want to clean up?${ENDCLR}" 
  echo ""
  echo "${OP}1) No, Leave everything.${ENDCLR}"
  echo "${OP}2) Yes, just delete the un-needed files for me.${ENDCLR}"
  echo "${OP}3) Yes, but keep the .config file for the future.${ENDCLR}"
  echo "${OP}Enter the number of your choice${ENDCLR}"
  read DELETE
  echo $DELETE

  if [[ "$DELETE" == "2" ]]; then
    echo "Deleting Source"
    sleep 2
  fi

  if [[ "$DELETE" == "3" ]]; then
    
    echo "${OP}If you want to use the config file from this kernel in the furture, it is located in your home folder under the name of config-${KERNEL_VER}${ENDCLR}"
    sleep 2
  fi

  # ALL DONE
  echo "${WARN}All Done! Your new kernel has been installed!${ENDCLR}"

  ) 2>&1 | tee -a AKM.log
}

################################################################################
###~MORE ARGUMENTS~###
################################################################################

if [[ "$1" == "-d" ]]; then
  echo "STARTING DRY RUN"
  DRYRUN
  exit 0
fi 

################################################################################
###~EXECUTION~###
################################################################################

# Make sure your in the folder where a .tar.bz2 downloaded from kernel.org is
# MAKE SURE YOUR ROOT WHEN RUNNING THIS!

date >> AKM.log #Add time to log


CHECKS 

(
# Warning
echo ""
echo "${WARN}Make sure that you launched this program from a directory which has a .tar.bz2 downloaded from kernel.org. If you didn't do this right, KILL this program!${ENDCLR}"

echo ""

# Ask and fill variables
echo "${OP}What is the version of the kernel you are trying to install? (e.g 2.6.0) MAKE SURE THIS IS RIGHT!${ENDCLR}"
read KERNEL_VER
echo $KERNEL_VER

# Decompress
echo "${OP}Decompressing Kernel...${ENDCLR}"
tar xivf linux-${KERNEL_VER}.tar.bz2

# Go into Kernel source
cd linux-$KERNEL_VER

CONFIG

MAKER

POST_INSTALL

cd ..

CLEAN

# ALL DONE
echo "${WARN}All Done! Your new kernel has been installed!${ENDCLR}"

) 2>&1 | tee -a AKM.log
